package com.jimmy.medical.controller;

import com.jimmy.medical.common.StringUtils;
import com.jimmy.medical.entity.User;
import com.jimmy.medical.service.IUserService;
import com.jimmy.medical.util.AjaxUtils;
import com.jimmy.medical.util.Func_T;
import com.jimmy.medical.model.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class LoginController {
    @Autowired
    private IUserService userService;

    @RequestMapping("/login")
    @ResponseBody
    public AjaxResult<User> login(final String username, final String password) {
        AjaxResult<User> result = new AjaxResult<User>();
        if(StringUtils.isBlank(username)) {
            result.setCode("401");
            result.setMessage("用户名为空");
        } else if(StringUtils.isBlank(password)) {
            result.setCode("402");
            result.setMessage("密码为空");
        } else {
            result = AjaxUtils.process(new Func_T<User>() {
                @Override
                public User invoke() throws Exception {
                    return userService.checkUser(username, password);
                }
            });


            if(result.getData() == null) {
                result.setCode("404");
                result.setMessage("用户名或密码错误");
            } else {
                result.setCode("200");
            }
        }

        return result;
    }
}
